/*
 *	Calculators
 *	2014-05-11 usp
 */

function openCalculatorWindow ( path, url, name, w, h ) 
{
	if ( w == null ) w = 200;
	if ( h == null ) h = 200;
	var wnd = window.open( path + 'framewnd.htm?load="' + url + '"', name, 'menubar=0, resizable=1, status=0, toolbar=0, width=' + w + ', height=' + h );
}

function calculatorKeyPress ( evt, targetFunction )
{
	if ( evt == null ) return;
	// Note: Firefox has charCode.
	if ( evt.keyCode == 13 || evt.charCode == 13  ) targetFunction ( ) ;
}
 
function imperialMetricConverter ( )
{
	// Last update: 2014-05-10
	// Find input controls.
	var mmBox = document.getElementById ( "imc-mm" );
	var inchBox = document.getElementById ( "imc-inch" );
	var feetBox = document.getElementById ( "imc-feet" );
	// Find out what has changed, and calculate others.
	if ( mmBox.value !== mmBox.lastValue ) {
		inchBox.value = Math.round ( eval( mmBox.value ) / 25.4 * 1000 ) / 1000 ;
		feetBox.value = Math.round ( inchBox.value / 12 * 1000 ) / 1000 ;
	}
	else if ( inchBox.value !== inchBox.lastValue ) {
		mmBox.value = Math.round ( eval ( inchBox.value ) * 25.4 * 100 ) / 100 ;
		feetBox.value = Math.round ( inchBox.value / 12 * 1000 ) / 1000 ;
	}
	else if ( feetBox.value !== feetBox.lastValue ) {
		inchBox.value = Math.round ( eval ( feetBox.value ) * 12 * 100 ) / 100 ;
		mmBox.value = Math.round ( eval ( feetBox.value ) * 12 * 25.4 * 100 ) / 100 ;
	}
	// Copy value lastValue for change detection.
	inchBox.lastValue = inchBox.value;	
	mmBox.lastValue = mmBox.value;	
}

function imperialMetricConverterClear ( )
{
	document.getElementById ( "imc-mm" ).lastValue = "" ;
	document.getElementById ( "imc-inch" ).lastValue = "" ;
	document.getElementById ( "imc-feet" ).lastValue = "" ;
}

function simpleDomeCalculator ( )
{
	// Get the input elements.
	var lengthBox = document.getElementById ( "simpleDome-Length" ) ;
	var heightBox = document.getElementById ( "simpleDome-Height" ) ;
	var radiusBox = document.getElementById ( "simpleDome-Radius" ) ;
	// Determine what to calculate.
	if ( lengthBox.value == "" || lengthBox.value == "NaN" ) {
		lengthBox.calculate = true;
		heightBox.calculate = false;
		radiusBox.calculate = false;
	} else if ( heightBox.value == "" || heightBox.value == "NaN"  ) {
		heightBox.calculate = true;
		lengthBox.calculate = false;
		radiusBox.calculate = false;
	} else if ( radiusBox.value == "" || radiusBox.value == "NaN"  ) {
		radiusBox.calculate = true;
		heightBox.calculate = false;
		lengthBox.calculate = false;
	}
	// Calculate the missing values.
	if ( radiusBox.calculate ) {
		var domeRadius = eval ( heightBox.value ) + Math.sqrt ( Math.pow( eval( radiusBox.value ), 2 ) - Math.pow( eval( lenghtBox.value ) / 2, 2 ));
		radiusBox.value = Math.round ( domeRadius * 100 ) / 100 ;
	}
	else if ( heightBox.calculate ) {
		var domeHeight = eval ( radiusBox.value ) - Math.sqrt ( Math.pow ( eval ( radiusBox.value ), 2 ) - Math.pow (( eval ( lengthBox.value ) / 2 ), 2 )) ;
		heightBox.value = Math.round ( domeHeight * 1000 ) / 1000 ;
	}
	else if ( lengthBox.calculate ) {
		var bodyLength =  2 * Math.sqrt ( Math.pow( eval( radiusBox.value ), 2 ) - Math.pow( eval( radiusBox.value ) - eval( heightBox.value ), 2 ));
		lengthBox.value = Math.round ( bodyLength * 100 ) / 100 ;
	}
	else {
		alert ( "Nothing to calculate..." );
	}
}

function simpleDomeCalculatorReset ( )
{
	document.getElementById ( "simpleDome-Length" ).lastValue = "" ;
	document.getElementById ( "simpleDome-Height" ).lastValue = "" ;
	document.getElementById ( "simpleDome-Radius" ).lastValue = "" ;
}

function enhancedDomeCalculator ( )
{

}

